﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using NUnit.Framework;

namespace Demo
{
    [TestFixture]
    public class ObservableTests
    {
        [Test]
        public void Constructor_EmptyObservable_AddsNoRows()
        {
            var x = new ObsClass(Observable.Empty<int>());
            Assert.AreEqual(1, x.Count);
        }

        [Test]
        public void Constructor_SingleValue_AddsProperNumRows()
        {
            var x = new ObsClass(Observable.Return(32));
            Assert.AreEqual(2, x.Count);
        }

        [Test]
        public void Constructor_OnException_ClearsList()
        {
            var x = new ObsClass(Observable.Throw<int>(new FormatException()));
            Assert.AreEqual(0, x.Count);
        }

        [Test]
        public void Constructor_ObservableNeverEnds_ListIsEmpty()
        {
            var x = new ObsClass(Observable.Never<int>());
            Assert.AreEqual(0, x.Count);
        }

        [Test]
        public void Constructor_ManyValues_AddsProperNumRows()
        {
            var x = new ObsClass(Observable.Range(1, 32));
            Assert.AreEqual(33, x.Count);
        }

        [Test]
        public void Constructor_ManyValues2_AddsProperNumRows()
        {
            var generated = Observable.Generate(
                5,				//initial state
                i => i < 15,    //condition
                i => i + 3,     //iteration
                i => i);  //result selector
            var x = new ObsClass(generated);
            Assert.AreEqual(5, x.Count);
        }


    }

    public class ObsClass
    {
        public List<int> lst;
        public ObsClass(IObservable<int> pObservable )
        {
            lst = new List<int>();
            pObservable.Subscribe(
                x => lst.Add(x),
                err => lst.Clear(),
                () => lst.Add(-1));
        }

        public void Clear()
        {
            lst.Clear();
        }

        public int Count
        {
            get { return lst.Count; }
        }
    }
}
